package jnpf.controller;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.service.ZXGL_OrganizeService;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.service.VisualdevService;
import jnpf.base.util.VisualUtils;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.engine.service.FlowTaskService;
import jnpf.entity.ZXGL_DuBanRenWuEntity;
import jnpf.entity.ZXGL_XiangMuFenJie_mingxibiaoEntity;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.generater.util.GeneraterSwapUtil;
import jnpf.model.common.ZXGL_Leaders;
import jnpf.model.zxgl_dubanrenwu.*;
import jnpf.service.ZXGL_DuBanRenWuService;
import jnpf.service.ZXGL_XiangMuFenJie_mingxibiaoService;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * ZXGL_DuBanRenWu
 *
 * @版本： V2.0
 * @版权： 河北交投智能交通技术有限责任公司
 * @作者： 数字交投项目组
 * @日期： 2025-03-21
 */
@Slf4j
@RestController
@Api(tags = "任务督办", value = "zxgl")
@RequestMapping("/ZXGL_DuBanRenWu")
public class ZXGL_DuBanRenWuController {


    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private VisualdevService visualdevService;

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private ZXGL_DuBanRenWuService zXGL_DuBanRenWuService;

    @Autowired
    private ZXGL_XiangMuFenJie_mingxibiaoService zxglXiangMuFenJieMingxibiaoService;

    @Autowired
    private ZXGL_OrganizeService zxglOrganizeService;


    /**
     * 列表
     *
     * @param zXGL_DuBanRenWuPagination
     * @return
     */
    @ApiOperation("任务督办列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZXGL_DuBanRenWuPagination zXGL_DuBanRenWuPagination) throws IOException {
        List<ZXGL_DuBanRenWuEntity> list = zXGL_DuBanRenWuService.getList(zXGL_DuBanRenWuPagination);
        //处理id字段转名称，若无需转或者为空可删除
        for (ZXGL_DuBanRenWuEntity entity : list) {
            entity.setFqr(generaterSwapUtil.userSelectValues(entity.getFqr()));
            entity.setZzr(generaterSwapUtil.userSelectValues(entity.getZzr()));
        }
        List<ZXGL_DuBanRenWuListVO> listVO = JsonUtil.getJsonToList(list, ZXGL_DuBanRenWuListVO.class);
        for (ZXGL_DuBanRenWuListVO zXGL_DuBanRenWuVO : listVO) {
        }


        for (ZXGL_DuBanRenWuListVO vo : listVO) {
            FlowTaskEntity flowTaskEntity = flowTaskService.getInfoSubmit(vo.getId());
            if (flowTaskEntity != null) {
                vo.setFlowState(flowTaskEntity.getStatus());
            } else {
                vo.setFlowState(null);
            }
        }
        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zXGL_DuBanRenWuPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }


    /**
     * 创建
     *
     * @param zXGL_DuBanRenWuCrForm
     * @return
     */
    @ApiOperation("添加任务督办信息")
    @PostMapping
    @Transactional
    public ActionResult create(@RequestBody @Valid ZXGL_DuBanRenWuCrForm zXGL_DuBanRenWuCrForm) throws DataException {
        String visualId = "672828507906143749";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        String mainId = RandomUtil.uuId();
        UserInfo userInfo = userProvider.get();
        ZXGL_DuBanRenWuEntity entity = JsonUtil.getJsonToBean(zXGL_DuBanRenWuCrForm, ZXGL_DuBanRenWuEntity.class);
        entity.setId(mainId);
        entity.setCreatorTime(new Date());

        // region 赋值部门负责人和领导
        ZXGL_Leaders leaders = zxglOrganizeService.getLeaders();
        entity.setDepartmentManager(leaders.getDepartmentManager());
        entity.setDepartmentHeadleader(leaders.getDepartmentHeadleader());

        ZXGL_XiangMuFenJie_mingxibiaoEntity model = zxglXiangMuFenJieMingxibiaoService.getInfo(entity.getRwid());
        entity.setDepartmentManagerZzbm(model.getDepartmentManagerZzbm());
        entity.setDepartmentHeadleaderZzbm(model.getDepartmentHeadleaderZzbm());
        // endregion

        zXGL_DuBanRenWuService.save(entity);
        if (FlowStatusEnum.submit.getMessage().equals(zXGL_DuBanRenWuCrForm.getStatus())) {
            try {
                VisualUtils.submitFlowTask(visualdevEntity, mainId, zXGL_DuBanRenWuCrForm, userInfo, zXGL_DuBanRenWuCrForm.getCandidateList());
                FlowTaskEntity flowTaskEntity = flowTaskService.getInfo(mainId);
                if (flowTaskEntity != null) {
                    flowTaskEntity.setTitle(zXGL_DuBanRenWuCrForm.getRwmc() + "-任务督办-" + userInfo.getUserName() + "-" + DateUtil.today());
                    flowTaskEntity.setDataTable("zxgl_dubanrenwu");
                    flowTaskService.update(flowTaskEntity);
                }
            } catch (WorkFlowException e) {
                e.printStackTrace();
            }
            return ActionResult.success("提交成功，请耐心等待");
        }
        return ActionResult.success("保存成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("获取任务督办信息")
    @GetMapping("/{id}")
    public ActionResult<ZXGL_DuBanRenWuInfoVO> info(@PathVariable("id") String id) {
        ZXGL_DuBanRenWuEntity entity = zXGL_DuBanRenWuService.getInfo(id);
        ZXGL_DuBanRenWuInfoVO vo = JsonUtil.getJsonToBean(entity, ZXGL_DuBanRenWuInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @return
     */
    @ApiOperation("更新任务督办信息")
    @PutMapping("/{id}")
    @Transactional
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZXGL_DuBanRenWuUpForm zXGL_DuBanRenWuUpForm) throws DataException {
        String visualId = "672828507906143749";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        UserInfo userInfo = userProvider.get();
        ZXGL_DuBanRenWuEntity entity = zXGL_DuBanRenWuService.getInfo(id);
        if (entity != null) {
            ZXGL_DuBanRenWuEntity subentity = JsonUtil.getJsonToBean(zXGL_DuBanRenWuUpForm, ZXGL_DuBanRenWuEntity.class);


            // region 赋值部门负责人和领导
            ZXGL_Leaders leaders = zxglOrganizeService.getLeaders();
            entity.setDepartmentManager(leaders.getDepartmentManager());
            entity.setDepartmentHeadleader(leaders.getDepartmentHeadleader());

            ZXGL_XiangMuFenJie_mingxibiaoEntity model = zxglXiangMuFenJieMingxibiaoService.getInfo(entity.getRwid());
            entity.setDepartmentManagerZzbm(model.getDepartmentManagerZzbm());
            entity.setDepartmentHeadleaderZzbm(model.getDepartmentHeadleaderZzbm());
            // endregion

            zXGL_DuBanRenWuService.update(id, subentity);

            if (FlowStatusEnum.submit.getMessage().equals(zXGL_DuBanRenWuUpForm.getStatus())) {
                try {
                    VisualUtils.submitFlowTask(visualdevEntity, id, subentity, userInfo, zXGL_DuBanRenWuUpForm.getCandidateList());
                    FlowTaskEntity flowTaskEntity = flowTaskService.getInfo(id);
                    if (flowTaskEntity != null) {
                        flowTaskEntity.setTitle(zXGL_DuBanRenWuUpForm.getRwmc() + "-任务督办-" + userInfo.getUserName() + "-" + DateUtil.today());
                        flowTaskEntity.setDataTable("zxgl_dubanrenwu");
                        flowTaskService.update(flowTaskEntity);
                    }
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
                return ActionResult.success("提交成功，请耐心等待");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除任务督办信息")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZXGL_DuBanRenWuEntity entity = zXGL_DuBanRenWuService.getInfo(id);
        if (entity != null) {
            FlowTaskEntity taskEntity = flowTaskService.getInfoSubmit(id);
            if (taskEntity != null) {
                try {
                    flowTaskService.delete(taskEntity);
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
            }
            zXGL_DuBanRenWuService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }


}
